package u.can.i.up.db.postgresql;

import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import u.can.i.up.db.mongodb.AppInfoByPlayDrone;
import u.can.i.up.db.mysql.ReadMysql;
import u.can.i.up.db.mysql.category.InitialUtils;

public class WriteAppInfo {

	static Logger logger = LogManager.getLogger();

	public static void writeAppInfo() {
		Reader reader;
		try {
			reader = Resources.getResourceAsReader("MybatisConfig.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader, "postgresql");
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AppInfoDAO appinfoDAO = sqlSession.getMapper(AppInfoDAO.class);
			AppCategoryDAO appCategory = sqlSession.getMapper(AppCategoryDAO.class);

			Map<String, Long> downloadNumberMap = new HashMap<String, Long>();
			Map<String, String> categoryMap = new HashMap<String, String>();

			logger.info("AppInfoByPlayDrone.readInfo(downloadNumberMap, categoryMap);");
			AppInfoByPlayDrone.readInfo(downloadNumberMap, categoryMap);
			logger.info("ReadMysql.readDownload(downloadNumberMap, categoryMap);");
			ReadMysql.readDownload(downloadNumberMap, categoryMap);
			logger.info("categoryMap = InitialUtils.initial(categoryMap);");
			categoryMap = InitialUtils.initial(categoryMap);

			logger.info("List<String> appPackageNameList = appinfoDAO.getAppList();");
			List<String> appPackageNameList = appinfoDAO.getAppList();
			for (String pkgName : appPackageNameList) {
				if (downloadNumberMap.get(pkgName) == null) {
					downloadNumberMap.put(pkgName, 0l);
				}
				int downloadNumber = (int) ((downloadNumberMap.get(pkgName) > 2147483647) ? 2147483647
						: downloadNumberMap.get(pkgName));
				String category = categoryMap.get(pkgName);
				if (category == null || category.isEmpty() || category.equals("")) {
					category = "其他";
				}
				Integer categoryID = appCategory.selectAppCategoryID(category);
				if (categoryID == null) {
					appCategory.insertAppCategory(category);
					categoryID = appCategory.selectAppCategoryID(category);
					sqlSession.commit();
				}
				// 判断pkg是否已经存在于AppInfo中
				Integer number = appinfoDAO.selectAppInfo(pkgName);
				if (number == null) {
					// pkgName不存在于AppInfo，执行insert操作
					appinfoDAO.insertAppInfo(pkgName, downloadNumber, categoryID);
				} else {
					// pkgName已存在，执行update操作
					appinfoDAO.updateAppInfo(pkgName, downloadNumber, categoryID);
				}
				sqlSession.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeAppInfoAll() {
		Reader reader;
		try {
			reader = Resources.getResourceAsReader("MybatisConfig.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader, "postgresql");
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AppInfoDAO appinfoDAO = sqlSession.getMapper(AppInfoDAO.class);
			AppCategoryDAO appCategory = sqlSession.getMapper(AppCategoryDAO.class);

			Map<String, Long> downloadNumberMap = new HashMap<String, Long>();
			Map<String, String> categoryMap = new HashMap<String, String>();

			logger.info("AppInfoByPlayDrone.readInfo(downloadNumberMap, categoryMap);");
			AppInfoByPlayDrone.readInfo(downloadNumberMap, categoryMap);
			logger.info("ReadMysql.readDownload(downloadNumberMap, categoryMap);");
			ReadMysql.readDownload(downloadNumberMap, categoryMap);
			logger.info("categoryMap = InitialUtils.initial(categoryMap);");
			categoryMap = InitialUtils.initial(categoryMap);
			for (Entry<String, Long> en : downloadNumberMap.entrySet()) {
				String pkgName = en.getKey();
				int downloadNumber = (en.getValue() > 2147483647) ? 2147483647 : en.getValue().intValue();
				String category = categoryMap.get(pkgName);
				if (category == null || category.isEmpty() || category.equals("")) {
					category = "其他";
				}
				Integer categoryID = appCategory.selectAppCategoryID(category);
				if (categoryID == null) {
					appCategory.insertAppCategory(category);
					categoryID = appCategory.selectAppCategoryID(category);
					sqlSession.commit();
				}

				// 判断pkg是否已经存在于AppInfo中
				Integer number = appinfoDAO.selectAppInfo(pkgName);
				if (number == null) {
					// pkgName不存在于AppInfo，执行insert操作
					appinfoDAO.insertAppInfo(pkgName, downloadNumber, categoryID);
				} else {
					// pkgName已存在，执行update操作
					appinfoDAO.updateAppInfo(pkgName, downloadNumber, categoryID);
				}
				sqlSession.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
